package com.bigShen.dbf;

import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

/**
 * @author : lei.shen
 * @date : 2018/10/25
 */
public class DbfTest {

    private static final String PATH = "E:\\压测\\500W（2500账套）";
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static Queue<String> queue = new LinkedList<String>();
    private static Integer SIZE = 0;

    static {
        File file = new File(PATH);
        if(file.isDirectory()) {
            File[] files = file.listFiles();
            SIZE = files.length;
            for (File element : files) {
                queue.offer(element.getPath());
            }
        }
    }

    public static void analysisDBF(String path) {
        InputStream fis = null;
        try {

            // 读取文件的输入流
            fis = new FileInputStream(path);
            // 根据输入流初始化一个DBFReader实例，用来读取DBF文件信息
            DBFReader reader = new DBFReader(fis);
            // 调用DBFReader对实例方法得到path文件中字段的个数
            int fieldsCount = reader.getFieldCount();
            // 取出字段信息
            for (int i = 0; i < fieldsCount; i++) {
                DBFField field = reader.getField(i);
                //System.out.println(field.getName());
            }
            Object[] rowValues;
            // 一条条取出path文件中记录
            while ((rowValues = reader.nextRecord()) != null) {
                //for (int i = 0; i < rowValues.length; i++) {
                //    System.out.println(rowValues);
                //    System.out.println(rowValues[i]);
                //}
            }

        } catch (Exception e) {
            System.out.println(String.format("dbf文件解析出错, %s , %s", path, e.toString()));
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }

    public static void main3(String[] args) {
        System.out.println("start: " + sdf.format(new Date()));
        analysisDBF("E:\\压测\\500W（2500账套）\\PAR_BOND_INFO20180103.dbf");
        System.out.println("end: " + sdf.format(new Date()));

    }

    public static void main(String[] args) {

        System.out.println("start: " + sdf.format(new Date()));

        List<Future<Long>> list = new ArrayList<Future<Long>>();

        ExecutorService pool = Executors.newFixedThreadPool(4);

        for (int i = 0; i < SIZE; i++) {

            list.add(pool.submit(new Callable<Long>() {

                public Long call() throws Exception {
                    Long start = System.currentTimeMillis();

                    String fileName = queue.poll();
                    System.out.println(String.format("%s 开始解析 %s %s", Thread.currentThread().getName(), fileName, sdf.format(new Date())));
                    analysisDBF(fileName);
                    System.out.println(String.format("%s 解析结束 %s %s ", Thread.currentThread().getName(), fileName, sdf.format(new Date())));

                    Long end = System.currentTimeMillis();
                    return end - start;
                }

            }));

        }

        pool.shutdown();
        Long time = 0L;
        for (Future<Long> future : list) {
            try {
                time += future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println(String.format("总耗时(ms) %s (s) %s", time, time/1000));
        System.out.println("end: " + sdf.format(new Date()));

    }


}
